import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:ball_lion/entity/other/resource_entity.dart';
import 'package:ball_lion/server/api/other_api.dart';
import 'package:ball_lion/utils/widget_utils.dart';
import 'package:ball_lion/module/live/items/live_message_list.dart';

import 'package:ball_lion/utils/utils.dart';

class LiveAds extends StatefulWidget {
  const LiveAds({super.key, required this.scene});

  final MessageScene scene;

  @override
  State<LiveAds> createState() => _LiveAdsState();
}

class _LiveAdsState extends State<LiveAds> {
  List<ResourceEntity>? liveAds;
  List<bool>? show;

  Future getLiveAds() async {
    List<ResourceEntity>? data = await OtherApi.getAppList("room_chat");
    liveAds = data?.where((element) => element.button == "ads").toList();
    show = liveAds?.map((e) => true).toList();
    setState(() {});
  }

  @override
  void initState() {
    getLiveAds();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return liveAds.noValue
        ? Container()
        : Column(
            mainAxisSize: MainAxisSize.min,
            children: List<Widget>.generate(
                liveAds?.length ?? 0,
                (index) => !(show?[index] ?? true)
                    ? Container()
                    : Column(
                        mainAxisSize: MainAxisSize.min,
                        crossAxisAlignment: CrossAxisAlignment.end,
                        children: [
                            Image.asset(
                                    width: 16,
                                    Utils.getImgPath("search_close.png"))
                                .tap(
                                    () => setState(() => show?[index] = false)),
                            GestureDetector(
                              onTap: () {
                                Utils.onEvent(
                                    widget.scene == MessageScene.normal
                                        ? 'spzb'
                                        : 'hpzb',
                                    params: {
                                      (widget.scene == MessageScene.normal
                                          ? 'spzb'
                                          : 'hpzb'): 'ads_$index'
                                    });
                                if (liveAds![index].href != null) {
                                  Utils.doJump(liveAds![index].href!);
                                }
                              },
                              child: CachedNetworkImage(
                                width: 60,
                                height: 60,
                                fit: BoxFit.cover,
                                imageUrl: liveAds![index].imgUrl ?? "",
                                errorWidget: (context, url, error) =>
                                    Container(),
                              ),
                            ),
                          ])).insertEvery(Container(height: 4)));
  }
}
